package com.example.spinning.controller;

import com.example.spinning.bean.*;
import com.example.spinning.service.*;
import com.example.spinning.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
纺纱台账管理
 */
@RestController
@RequestMapping("/spinningParameterController")
public class SpinningParameterController {

    @Autowired
    private SpinningFeedingService spinningFeedingService;

    @Autowired
    private SpinningSummaryService spinningSummaryService;

    private Logger logger = LoggerFactory.getLogger(getClass());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //得到所有的投料单
    @Log(module = "投料单管理",description = "得到所有投料单信息")
    @RequestMapping(path = "/allSpinningFeeding",method = RequestMethod.GET)
    public Map<String,Object> getSpinningFeeding(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = spinningFeedingService.getSpinningFeedingCount();
        List<SpinningFeeding> spinningFeeding = spinningFeedingService.getAllSpinningFeeding(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("spinningFeeding",spinningFeeding);
        return map;
    }

    //得到所有的纺纱汇总表
    @Log(module = "纺纱汇总表管理",description = "得到所有纺纱汇总表信息")
    @RequestMapping(path = "/allSpinningSummary",method = RequestMethod.GET)
    public Map<String,Object> allSpinningSummary(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = spinningSummaryService.getSpinningSummaryCount();
        List<SpinningSummary> spinningSummary = spinningSummaryService.getAllSpinningSummary(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("spinningSummary",spinningSummary);
        return map;
    }

    //通过id查找投料单
    @Log(module = "投料单管理",description = "根据id查询投料单")
    @RequestMapping(path = "/getSpinningFeedingById/",method = RequestMethod.GET)
    public SpinningFeeding getSpinningFeedingById(Long id){
        return spinningFeedingService.getSpinningFeedingById(id);
    }
    //通过id查找纺纱汇总表
    @Log(module = "纺纱汇总表管理",description = "根据id查询纺纱汇总表")
    @RequestMapping(path = "/getSpinningSummaryById/",method = RequestMethod.GET)
    public SpinningSummary getSpinningSummaryById(Long id){
        return spinningSummaryService.getSpinningSummaryById(id);
    }

    //通过条件模糊查找投料单
    @Log(module = "投料单管理",description = "模糊查询投料单")
    @RequestMapping(path = "/findFeedingByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> findFeedingByConditionLike(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String date,
            String batch_no,
            String num,
            String composition,
            String customer

    ){
        int totalCount = spinningFeedingService.getSpinningFeedingCountByCondition(date,batch_no,num,composition,customer);
        List<SpinningFeeding> spinningFeeding = spinningFeedingService.getSpinningFeedingByCondition(page,count,date,batch_no,num,composition,customer);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("spinningFeeding",spinningFeeding);
        return map;
    }

    //通过条件模糊查找纺纱汇总表
    @Log(module = "纺纱汇总表管理",description = "模糊查询纺纱汇总表")
    @RequestMapping(path = "/findSummaryByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> findSummaryByConditionLike(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String customer,
            String workshop,
            String date,
            String batch_no,
            String num,
            String composition

    ){
        int totalCount = spinningSummaryService.getSpinningSummaryCountByCondition(customer,workshop,date,batch_no,num,composition);
        List<SpinningSummary> spinningSummary = spinningSummaryService.getSpinningSummaryByCondition(page,count,customer,workshop,date,batch_no,num,composition);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("spinningSummary",spinningSummary);
        return map;
    }


    //添加投料单信息
    @Log(module = "投料单管理",description = "添加投料单记录")
    @RequestMapping(path = "/addSpinningFeeding/",method = RequestMethod.POST)
    public RespBean addSpinningFeeding(SpinningFeeding spinningFeeding) {
        int result = spinningFeedingService.addSpinningFeeding(spinningFeeding);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加纺纱汇总表信息
    @Log(module = "纺纱汇总表管理",description = "添加纺纱汇总表记录")
    @RequestMapping(path = "/addSpinningSummary/",method = RequestMethod.POST)
    public RespBean addSpinningSummary(SpinningSummary spinningSummary) {
        int result = spinningSummaryService.addSpinningSummary(spinningSummary);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }


    //修改投料单
    @Log(module = "投料单管理",description = "修改投料单信息")
    @RequestMapping(path = "/updateSpinningFeeding/",method = RequestMethod.PUT)
    public RespBean updateSpinningFeeding(SpinningFeeding spinningFeeding){
        int i = spinningFeedingService.updateSpinningFeeding(spinningFeeding);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改纺纱汇总表
    @Log(module = "纺纱汇总表管理",description = "修改纺纱汇总表信息")
    @RequestMapping(path = "/updateSpinningSummary/",method = RequestMethod.PUT)
    public RespBean updateSpinningSummary(SpinningSummary spinningSummary){
        int i = spinningSummaryService.updateSpinningSummary(spinningSummary);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }



    //删除投料单
    @Log(module = "投料单管理",description = "删除投料单信息")
    @RequestMapping(value = "/deleteSpinningFeedingById/",method = RequestMethod.DELETE)
    public RespBean deleteSpinningFeedingById(String ids){
        boolean result = spinningFeedingService.deleteSpinningFeedingByIds(ids);
        if(result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除纺纱汇总表
    @Log(module = "纺纱汇总表管理",description = "删除纺纱汇总表信息")
    @RequestMapping(value = "/deleteSpinningSummaryById/",method = RequestMethod.DELETE)
    public RespBean deleteSpinningSummaryById(String ids){
        boolean result = spinningSummaryService.deleteSpinningSummaryByIds(ids);
        if(result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

}
